package com.cysion.shell.vm

import com.cysion.otbox.base.BaseViewModel
import com.cysion.otbox.ext.*
import com.cysion.shell.net.TestCaller
import com.cysion.shell.resp.ErAddress
import com.cysion.shell.resp.base.Address
import com.cysion.shell.resp.base.CodeMsgResult
import com.cysion.shell.resp.base.StaResult
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.delay
import okhttp3.OkHttpClient
import okhttp3.Request
import java.lang.Exception


class MainViewModel : BaseViewModel() {


    fun getCodeMsg() {
        launchFun<CodeMsgResult<Address>, Address>({
            TestCaller.api.getCodeMsg()
        }, {
            logd("-------code-----------")
            logObj(it)
        })
        launchFun<CodeMsgResult<Address>,Address>(
            {TestCaller.api.getCodeMsg()}
        ,{

        })
        launchFun2<CodeMsgResult<Address>,Address>(
            {
                TestCaller.api.getCodeMsg()
            }
        ){

        }
    }

    fun getErrMsg() {
        launchFun<ErAddress, ErAddress>(
            {
                TestCaller.api.getErrMsg()
            }, {
                logd("-------err-----------")
                logObj(it)
            }, { s: String, s1: String ->
                logd("$s-$s1")
            })
    }

    fun getStaMsg() {
        launchFun<StaResult<Address>, Address>({
            val errMsg = TestCaller.api.getErrMsg()
            getObj()
        }, {
            logd("-------sta----------")
            logObj(it)
        }, { s: String, s1: String ->
            logd("$s-$s1")
        })
    }

    fun getObj(): StaResult<Address> {
        val caller = OkHttpClient.Builder().build()
        val req = Request.Builder()
            .url("https://cysion-box-1257966290.cos.ap-beijing.myqcloud.com/json/sta_msg.json")
            .get().build()
        val execute = caller.newCall(req).execute().body()
        val json = execute!!.string()
        return gson.fromJson<StaResult<Address>>(json)!!
    }


}